package com.hlbspringcloud.mq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

/**
 * @Author huanglibin
 * @Project mq
 * @Description 工作队列 公平分发 消息发送者
 * @Date 2020-12-02-16-23-11
 * @Version 1.0
 */
public class WorkFairSender {
    /**
     * 队列名称
     */
    public static final String QUEUE_NAME="work fair query";

    /**
     * 发送消息
     * @throws Exception
     */
    public void sendMessage() throws Exception{
        //1、获取连接
        Connection rabbitmqConnection = RabbitmqUntil.getRabbitmqConnection();
        //2、获取管道
        Channel channel = rabbitmqConnection.createChannel();
        //3、创建队列
        channel.queueDeclare(QUEUE_NAME,false,false,false,null);
        //4、设置每次发送消息不超过1条
        channel.basicQos(1);
        //5、发送消息
        for (int i = 0; i < 50; i++) {
            String message="测试工作队列之公平分发"+i;
            System.out.println("发布信息："+message);
            channel.basicPublish("",QUEUE_NAME,null,message.getBytes());
        }
    }
    public static void main(String[] args)throws Exception {
        WorkFairSender workFairSender = new WorkFairSender();
        workFairSender.sendMessage();
    }
}
